package org.abettor.jroa.impl;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.abettor.jroa.Acceptor;
import org.abettor.jroa.AuthenticationException;
import org.abettor.jroa.Connection;

/**
 * <p>The implemention of the <i>Acceptor</i> interface
 * implemented by <i>ServerSocket</i>.</p>
 * 
 * @see org.abettor.jroa.Acceptor
 * @see java.net.ServerSocket
 * 
 * @author <a href="http://www.abettor.org/" target="_blank">abettor</a>
 *
 */
public class SocketAcceptor implements Acceptor
{
	private ServerSocket socket;

	/**
	 * Construct a <i>Acceptor</i> object with a <i>ServerSocket</i> object.
	 * @param socket An bound <i>ServerSocket</i> object.
	 */
	public SocketAcceptor(ServerSocket socket)
	{
		this.socket = socket;
	}

	/**
	 * @see org.abettor.jroa.Acceptor#accept()
	 */
	public Connection accept() throws IOException, AuthenticationException
	{
		Socket sock = socket.accept();

		return new SocketConnection(sock);
	}

	/**
	 * @see org.abettor.jroa.Acceptor#isClosed()
	 */
	public boolean isClosed()
	{
		if(socket.isClosed())
		{
			return true;
		}
		else if(! socket.isBound())
		{
			return true;
		}

		return false;
	}

	/**
	 * @see java.net.ServerSocket#close()
	 */
	public void close() throws IOException
	{
		if(! socket.isClosed())
		{
			socket.close();
		}
	}

}
